package com.ys.controller;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ys.common.ApiRestResponse;
import com.ys.exception.YsLjExceptionEnum;
import com.ys.model.pojo.Component;
import com.ys.model.pojo.Equipment;
import com.ys.model.vo.*;
import com.ys.model.request.RepairSearchReq;
import com.ys.model.request.UpComponentReq;
import com.ys.service.ComponentService;
import com.ys.service.EquipmentService;
import com.ys.service.QrCodeService;
import com.ys.util.DateUtils;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 *ComponentController
 */
@RestController
@RequestMapping("/component")
public class ComponentController {
    @Autowired
    ComponentService componentService;
    @Autowired
    EquipmentService equipmentService;
    @Autowired
    QrCodeService qrCodeService;


    /*首页根据零件名称筛选手机端*/
    @PostMapping("/getInfosByCname")
    @ApiOperation(value = "首页-根据零件名称获得信息",notes = "零件名称cName")
    @ResponseBody
    public ApiRestResponse getInfosByCname(@Valid @RequestBody RepairSearchReq repairSearchReq) {
        List<BaseVo> byCname = componentService.getInfosByCname(repairSearchReq);
        return ApiRestResponse.success(byCname);

    }
    /**
     * 新增零件信息
     *
     */
    @ApiOperation(value = "零件新增",notes = "eId:(设备id)cName:零件名称(String)cType:零件类型 (String)cCode:零件的编码(String)" +
            "fName:零件厂家(String)cLocation:零件位置(String)lifespan:使用寿命(String)starttime:开始使用时间(String)")
    @PostMapping("/addComponent")
    @ResponseBody
    public ApiRestResponse addComponent(@Valid @RequestBody Component component)

    {
        /*if (component.getcCode().length() != 11) {
            return ApiRestResponse.error(YsLjExceptionEnum.ECODE_EXISTED);
        }
        for (int i = component.getcCode().length() ; --i >= 0; ) {
            if (!Character.isDigit(component.getcCode().charAt(i))) {
                return ApiRestResponse.error(YsLjExceptionEnum.ECODE_EXISTED);
            }
        }*/
        componentService.addComponent(component);
        return ApiRestResponse.success();
    }

    /**
     * 修改零件信息
     * @param upComponentReq
     * @return
     */
    @ApiOperation("修改零件信息")
    @PostMapping("/update")
    @ResponseBody
    public ApiRestResponse updateComponent(@Valid @RequestBody UpComponentReq upComponentReq) {

       /* if (upComponentReq.getcCode().length() != 11) {
            return ApiRestResponse.error(YsLjExceptionEnum.ECODE_EXISTED);
        }
        for (int i = upComponentReq.getcCode().length(); --i >= 0; ) {
            if (!Character.isDigit(upComponentReq.getcCode().charAt(i))) {
                return ApiRestResponse.error(YsLjExceptionEnum.ECODE_EXISTED);
            }
        }*/
        Component component = new Component();
        BeanUtils.copyProperties(upComponentReq,component);
        componentService.updateComponent(component);
        return ApiRestResponse.success();
    }


    @PostMapping("/allComponentList")
    @ApiOperation("零件列表")
    @ResponseBody
    public ApiRestResponse componentLists(@Valid @RequestBody RepairSearchReq repairSearchReq) {
        PageHelper.startPage(repairSearchReq.getPageNum(), repairSearchReq.getPageSize());
        PageInfo p =null;
        List<EquipmentChildrenVo> equipmentList = equipmentService.getEquipment(repairSearchReq);
        List<EquipmentChildrenVo> equipmentList2 = new ArrayList<>();

        boolean flag = false;
        for (EquipmentChildrenVo childrenVo : equipmentList) {
            int eId = childrenVo.geteId();
            String cName = repairSearchReq.getcName();
            List<ComponentChildrenVo> componentChildrenVos = componentService.componentList(eId, cName);

            if(!componentChildrenVos.isEmpty()){
                componentChildrenVos.stream().forEach(componentVo -> {
                    long now = new Date().getTime();
                    long start = componentVo.getStarttime().getTime();

                    String useTime = DateUtils.formatDateTime((now - start) / 1000);

                    componentVo.setRuntime(useTime);

                });
                childrenVo.setChildren(componentChildrenVos);
                childrenVo.setDev(false);
                if (repairSearchReq.getcName() != null) {
                    for (ComponentChildrenVo c : componentChildrenVos) {
                        if (childrenVo.geteId() == c.geteId()) {
                            equipmentList2.add(childrenVo);
                            flag = true;
                            break;
                        }
                    }
                }
            }else {
                childrenVo.setChildren(null);
                childrenVo.setDev(true);
            }

        }
        if(flag)
            p = new PageInfo(equipmentList2);
        else
            p = new PageInfo(equipmentList);
        return new ApiRestResponse().success(p);

    }

    /**
     * 删除零件数据
     * @param
     * @return
     */
    @ApiOperation("删除零件数据")
    @PostMapping("/deleteComponent")
    @ResponseBody
    public ApiRestResponse deleteCcomponentById(@RequestBody ComponentVo componentVo){
            componentService.delete(componentVo);
            return new ApiRestResponse().success();

    }

    @GetMapping("/searchList")
    @ApiOperation("设备零件查询下拉列表")
    public ApiRestResponse componentSearchList() {
        List<SearchVo> equipmentSearchList = equipmentService.equipmentSearchList();
        List<SearchVo> componentSearchList = componentService.allCName();
        //将机台、设备等对应放进一个list集合
        List<String> eWorkshops = new ArrayList<>();
        List<String> eMachines =  new ArrayList<>();
        List<String> eNames =  new ArrayList<>();
        for(SearchVo searchVo : equipmentSearchList){
            eWorkshops.add(searchVo.geteWorkshop());
            eMachines.add(searchVo.geteMachine());
            eNames.add(searchVo.geteName());
        }

        List<String> cNames =  new ArrayList<>();
        for(SearchVo searchVo : componentSearchList){
            cNames.add(searchVo.getcName());
        }

        JSONObject object1 = new JSONObject();
        object1.put("eWorkshops",eWorkshops);
        object1.put("eMachines",eMachines);
        object1.put("eNames",eNames);
        object1.put("cNames",cNames);

        return new ApiRestResponse().success(object1);
    }


}
